#ifndef TRADEHANDLER_H
#define TRADEHANDLER_H

#include <QObject>
#include "trade.h"
#include "config/config.h"
#include "db/db.h"
#include "tradeview.h"
#include "stock/stock.h"
#include "global/global.h"

#define NO_FLAG             0x00
#define DONT_INIT_VALUES    0x01    //do not load any values when initiated
#define SHOW_BUY_DIALOG     0x02

#define TRADE_STOCK_VALSYMBOLS  "l10,t10"   //the values, which will be shown in the tradeview
#define TRADE_VALSYMBOLS    "STOCK_ID,COUNT,BUY_DATE,SELL_DATE,BID,ASK,PROVISION,PROFIT,STATUS"
#define TRADE_VALNAMES  "Wertpapier,Anzahl,Kaufdatum,Verkaufsdatum,Verkaufskurs,Kaufkurs,Provision,Profit,Status"
#define TRADE_VALFIELDS QString(TRADE_VALSYMBOLS)+","+QString(STOCK_NAME_FIELD) //read additional the stock name with an inner join

class TradeHandler : public DB,public TradeView
{
    //Q_OBJECT
public:
    explicit TradeHandler(QObject *parent = 0);
    explicit TradeHandler(int flag,QObject *parent = 0);
    explicit TradeHandler(Config* cfg,int flag=NO_FLAG,QObject *parent=0);
    explicit TradeHandler(const QString& tableName,Config* cfg,int flag=NO_FLAG,QObject *parent=0);

    QList<Trade *> getAllTrades() const;
    QStringList getValSymbols()const;
    QStringList getValNames()const;

    Trade* getTrade(const QString& stockName);

    void addTrade(const QString& stockName,const QStringList& values,int flag=NO_FLAG);
    void addTrade(const QString& stockName,Trade* trade);
    void addTrade(Trade* trade,int flag=NO_FLAG);

    void setTableName(const QString& tableName);
    void setConfig(Config* cfg);

    void init();

    virtual void setStandardValue(const QString& key,const QVariant& val,int flag)=0;
    virtual QVariant getStandardValue(const QString& key)=0;

    virtual double getCurrentLiquidity()=0;
    virtual void setCurrentLiquidity(const qreal& liq)=0;


    Stock* getTradeStocks();

signals:
    
public slots:
protected:
    virtual Stock* getStocks()=0;

private:

    QMap<QString,Trade*> trades;        //all trades this handler handles with, key=StockName (or StockSymbol?)
    Config* cfg;                        //don't delete
   // QStringList valSymbols,valNames;    //value symbols of a trade
    QString tableName;                  //name of the db table, where trades stored
    int flag;
    Stock* stocks;
};

#endif // TRADEHANDLER_H
